﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Obligatorio.DataAccess;

public partial class Purchases_CashRegister : System.Web.UI.Page, ICashRegisterView
{
    #region Fields
    CashRegisterPresenter _presenter;
    #endregion

    #region Properties

    public short BranchId
    {
        get;
        set;
    }
    public long UserId { get { return Convert.ToInt64(CookieManager.GetValue(CommonValues.USER_ID_KEY)); } }

    public IEnumerable<Product> Products
    {
        set
        {
            var ddl = (DropDownList)DetailsView1.FindControl("ddlProducts");
            ddl.DataSource = value;
            ddl.DataBind();
        }
    }

    public IEnumerable<object> PruchaseProducts
    {
        set { throw new NotImplementedException(); }
    }

    public int ProductId
    {
        get
        {
            var ddl = (DropDownList)DetailsView1.FindControl("ddlProducts");
            return Convert.ToInt32(ddl.SelectedValue);
        }
    }

    public int Quantity
    {
        get
        {
            var txt = (TextBox)DetailsView1.FindControl("txtQuantity");
            return Convert.ToInt32(txt.Text);
        }
        private set
        { var txt = (TextBox)DetailsView1.FindControl("txtQuantity"); }
    }
    public long PurchaseId
    {
        get
        {
            if (!string.IsNullOrEmpty(hdnPurchase.Value))
                return Convert.ToInt64(hdnPurchase.Value);
            return 0;
        }
        set { hdnPurchase.Value = value.ToString(); }
    }

    #endregion


    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        _presenter = new CashRegisterPresenter(this);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.EnableDynamicData(typeof(ProductStock));
        this._presenter.GetUserBranch();
        if (!IsPostBack)
            this._presenter.GetProducts();

    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        this._presenter.InsertProductPurchase();
        this.GridView1.DataSourceID = null;
        this.GridView1.DataSource = null;
        this.GridView1.DataBind();
        this.GridView1.DataSourceID = "edsPurchaseProduct";
        this.edsPurchaseProduct.DataBind();
        this._presenter.GetProducts();
        this.Quantity = 0;
    }

    protected void imgAdd_Click(object sender, ImageClickEventArgs e)
    {
        this._presenter.CreatePurchase();
        this.DetailsView1.Visible = true;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            PurchaseProduct ps = (e.Row.DataItem as PurchaseProduct);
            var p = e.Row.DataItem.GetType();
            var productName = this._presenter.GetProduct(ps.ProductId);
            Label lbProductName = (Label)e.Row.FindControl("lblProduct");
            lbProductName.Text = productName;
        }
    }
}